package com.fireline.beauty.seting;

import java.text.DecimalFormat;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;

import com.fireline.beauty.jdbc.SqliteJdbcImpl;
import com.fireline.beauty.test.CnToSpell;
import com.zgw.fireline.base.controls.DataCombo;
import com.zgw.fireline.base.dataset.Dataset;
import com.zgw.fireline.base.frame.IFunction;
import com.zgw.fireline.base.widgets.Batch;
import com.zgw.fireline.base.widgets.BatchItem;

/**
 * 服务项目编辑
 * */
public class FfxmEdit implements IFunction {
	public static final int Add = 1;
	public static final int Edit = 2;
	private Text textXmbh;
	private Text textXmmc;
	private Text textDj;
	private Text textFwsj;
	private Text textBz;
	private Text textHfzq;
	private Text textHlzq;
	private int type = 1; // 1 添加 2 修改
	public int resultCode = 0;
	private Listener enabledListener; // 界面状态监听

	/**
	 * 
	 * @param type
	 *            编辑类别 1新增 2修改
	 * */
	public FfxmEdit(int type, Dataset fwxm) {
		this.type = type;
		if (type == Edit) {
			ffxmSet.populate(fwxm, fwxm.getSelectedIndex());
			ffxmSet.setSelected(0);
		}
	}

	/**
	 * @wbp.nonvisual location=67,482
	 */
	private final Dataset ffxmSet = new Dataset(true, "fwxm",
			SqliteJdbcImpl.INSTANCE);
	/**
	 * @wbp.nonvisual location=133,482
	 */
	private final Batch addFfxm = new Batch(SqliteJdbcImpl.INSTANCE);
	/**
	 * @wbp.nonvisual location=183,482
	 */
	private final Batch editFfxm = new Batch(SqliteJdbcImpl.INSTANCE);
	private Button btnJy;
	private Button btnKy;
	private DataCombo comboLb;
	/**
	 * @wbp.nonvisual location=27,482
	 */
	private final Dataset fwlb = new Dataset("select * from fwlb",
			SqliteJdbcImpl.INSTANCE);
	private Button btnSfhf;
	private Button btnSfyhlzq;
	private Label label_4;
	private Label label_5;
	private Button btnJxtj;
	private Control content;
	/**
	 * @wbp.nonvisual location=27,552
	 */
	private final Dataset nextId = new Dataset(
			"select max(xmbh)+1 nextId from  fwxm ", SqliteJdbcImpl.INSTANCE);

	/**
	 * @wbp.parser.entryPoint
	 */
	public Control createControl(Composite parent) {
		ffxmSet.setKeyColumns(new String[] {});
		fwlb.setKeyColumns(new String[] {});

		BatchItem batchItem = new BatchItem(editFfxm);
		batchItem
				.setCommand("update fwxm set\r\nxmmc=? ,jm=?,lb=?,jy=?,dj=?,fwsj=?\r\n,bz=? ,hf=?,hfzq=?,hlzq=?,zqsj=?\r\nwhere xmbh=?");
		batchItem.setName("修改服务项");

		BatchItem batchItem_1 = new BatchItem(addFfxm);
		batchItem_1
				.setCommand("Insert into fwxm (xmbh,xmmc,jm,dj,lb,bz,jy,hf,hfzq,hlzq,zqsj,fwsj)\r\n values(?,?,?,?,?,?,?,?,?,?,?,?) ");
		batchItem_1.setName("新增服务项目");
		nextId.setKeyColumns(new String[] {});
		final Composite content = new Composite(parent, SWT.NONE);
		content.setLayout(new GridLayout(1, false));

		Composite composite = new Composite(content, SWT.NONE);
		GridLayout gl_composite = new GridLayout(5, false);
		gl_composite.verticalSpacing = 10;
		gl_composite.marginRight = 10;
		gl_composite.marginWidth = 25;
		gl_composite.marginHeight = 15;
		composite.setLayout(gl_composite);
		composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false,
				1, 1));

		Label lblNewLabel = new Label(composite, SWT.NONE);
		lblNewLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		lblNewLabel.setText("项目编号：");

		textXmbh = new Text(composite, SWT.BORDER);
		textXmbh.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));
		new Label(composite, SWT.NONE);

		Label label_1 = new Label(composite, SWT.NONE);
		label_1.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false,
				1, 1));
		label_1.setText("项目名称：");

		textXmmc = new Text(composite, SWT.BORDER);
		textXmmc.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));

		Label label_2 = new Label(composite, SWT.NONE);
		label_2.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false,
				1, 1));
		label_2.setText("所属类别：");

		comboLb = new DataCombo(composite, SWT.READ_ONLY, fwlb);
		comboLb.setShowText("${lbmc}");
		comboLb.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1,
				1));
		comboLb.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));

		Button btnNewButton_2 = new Button(composite, SWT.NONE);
		btnNewButton_2.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false,
				false, 1, 1));
		btnNewButton_2.setToolTipText("添加类别");
		btnNewButton_2.setFont(SWTResourceManager.getFont("宋体", 9, SWT.NORMAL));
		btnNewButton_2.setText("+");

		Label label_3 = new Label(composite, SWT.NONE);
		label_3.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false,
				1, 1));
		label_3.setText("状态：");

		Composite composite_3 = new Composite(composite, SWT.NONE);
		composite_3.setLayout(new RowLayout(SWT.HORIZONTAL));

		btnKy = new Button(composite_3, SWT.RADIO);
		btnKy.setText("可用");

		btnJy = new Button(composite_3, SWT.RADIO);
		btnJy.setText("禁用");

		label_4 = new Label(composite, SWT.NONE);
		label_4.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false,
				1, 1));
		label_4.setText("单价：");

		textDj = new Text(composite, SWT.BORDER);
		textDj.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1,
				1));
		new Label(composite, SWT.NONE);

		label_5 = new Label(composite, SWT.NONE);
		label_5.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false,
				1, 1));
		label_5.setText("服务时间：");

		Composite composite_5 = new Composite(composite, SWT.NONE);
		GridLayout gl_composite_5 = new GridLayout(2, false);
		gl_composite_5.verticalSpacing = 0;
		gl_composite_5.marginWidth = 0;
		gl_composite_5.marginHeight = 0;
		gl_composite_5.horizontalSpacing = 0;
		composite_5.setLayout(gl_composite_5);
		composite_5.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false,
				false, 1, 1));

		textFwsj = new Text(composite_5, SWT.BORDER);
		textFwsj.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));

		Label lblNewLabel_3 = new Label(composite_5, SWT.NONE);
		lblNewLabel_3.setText(" 分钟");

		Label label_6 = new Label(composite, SWT.NONE);
		label_6.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false,
				1, 1));
		label_6.setText("备注信息：");

		textBz = new Text(composite, SWT.BORDER);
		textBz.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 4,
				1));

		Composite composite_4 = new Composite(content, SWT.NONE);
		GridLayout gl_composite_4 = new GridLayout(4, false);
		gl_composite_4.horizontalSpacing = 10;
		gl_composite_4.verticalSpacing = 10;
		gl_composite_4.marginHeight = 0;
		gl_composite_4.marginWidth = 25;
		composite_4.setLayout(gl_composite_4);
		composite_4.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false, 1, 1));

		btnSfhf = new Button(composite_4, SWT.CHECK);
		btnSfhf.setText("是否回访");

		final Label lblNewLabel_1 = new Label(composite_4, SWT.NONE);
		lblNewLabel_1.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true,
				false, 1, 1));
		lblNewLabel_1.setText("消费此服务项目：");

		textHfzq = new Text(composite_4, SWT.BORDER);
		GridData gd_textHfzq = new GridData(SWT.FILL, SWT.CENTER, false, false,
				1, 1);
		gd_textHfzq.widthHint = 60;
		textHfzq.setLayoutData(gd_textHfzq);

		final Label lblNewLabel_2 = new Label(composite_4, SWT.NONE);
		lblNewLabel_2.setText("天后进行回访");

		btnSfyhlzq = new Button(composite_4, SWT.CHECK);

		btnSfyhlzq.setText("是否有护理周期");

		final Label label_7 = new Label(composite_4, SWT.NONE);
		label_7.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false,
				1, 1));
		label_7.setText("此项目每隔：");

		textHlzq = new Text(composite_4, SWT.BORDER);
		GridData gd_textHlzq = new GridData(SWT.FILL, SWT.CENTER, false, false,
				1, 1);
		gd_textHlzq.widthHint = 60;
		textHlzq.setLayoutData(gd_textHlzq);

		final Label label_8 = new Label(composite_4, SWT.NONE);
		label_8.setText("天护理一次");

		Label label = new Label(content, SWT.SEPARATOR | SWT.HORIZONTAL);
		label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1,
				1));

		Composite composite_1 = new Composite(content, SWT.NONE);
		GridLayout gl_composite_1 = new GridLayout(2, false);
		composite_1.setLayout(gl_composite_1);
		composite_1.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false, 1, 1));

		enabledListener = new Listener() {
			@Override
			public void handleEvent(Event arg0) {
				lblNewLabel_1.setEnabled(btnSfhf.getSelection());
				textHfzq.setEditable(btnSfhf.getSelection());
				lblNewLabel_2.setEnabled(btnSfhf.getSelection());
				label_7.setEnabled(btnSfyhlzq.getSelection());
				textHlzq.setEditable(btnSfyhlzq.getSelection());
				label_8.setEnabled(btnSfyhlzq.getSelection());
			}
		};
		btnSfyhlzq.addListener(SWT.Selection, enabledListener);
		btnSfhf.addListener(SWT.Selection, enabledListener);
		btnJxtj = new Button(composite_1, SWT.CHECK);
		btnJxtj.setText("保存后继续添加");
		btnJxtj.setVisible(type == 1);

		Composite composite_2 = new Composite(composite_1, SWT.NONE);
		composite_2.setLayout(new RowLayout(SWT.HORIZONTAL));
		composite_2.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true,
				false, 1, 1));

		Button btnSave = new Button(composite_2, SWT.NONE);
		btnSave.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				doSave();
			}
		});
		btnSave.setLayoutData(new RowData(80, SWT.DEFAULT));
		btnSave.setText("保存&S");

		Button btnExit = new Button(composite_2, SWT.NONE);
		btnExit.setLayoutData(new RowData(80, SWT.DEFAULT));
		btnExit.setBounds(0, 0, 72, 22);
		btnExit.setText("退出&E");
		btnExit.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				resultCode = SWT.CANCEL;
				content.dispose();
			}
		});
		createGUIAfter();
		setParams();
		setValues();
		enabledListener.handleEvent(new Event());
		this.content = content;
		return content;
	}

	protected void doSave() {
		setParams();
		if (type == Edit) {
			editFfxm.setParam(0, 1,
					CnToSpell.getPinYinHeadChar(textXmmc.getText()));
			editFfxm.execute();
			MessageDialog.openInformation(content.getShell(), "操作提示", "保存成功");
			resultCode = SWT.OK;
			content.dispose();
		} else if (type == Add) {
			addFfxm.execute();
			MessageDialog.openInformation(content.getShell(), "操作提示", "保存成功");
			if (btnJxtj.getSelection()) {
				// 清空值
				setValues();
			} else {
				resultCode = SWT.OK;
				content.dispose();
			}
		}
	}

	public void clear() {

	}

	protected void setParams() {
		editFfxm.setParam(0, 0, textXmmc.getText());
		editFfxm.setParam(0, 1, textXmmc.getText());
		editFfxm.setParam(0, 2, comboLb.getText());
		editFfxm.setParam(0, 3, btnJy.getSelection());
		editFfxm.setParam(0, 4, textDj.getText());
		editFfxm.setParam(0, 5, textFwsj.getText());
		editFfxm.setParam(0, 6, textBz.getText());
		editFfxm.setParam(0, 7, btnSfhf.getSelection());
		editFfxm.setParam(0, 8, textHfzq.getText());
		editFfxm.setParam(0, 9, btnSfyhlzq.getSelection());
		editFfxm.setParam(0, 10, textHlzq.getText());
		editFfxm.setParam(0, 11, ffxmSet.getValue("xmbh"));

		addFfxm.setParam(0, 0, textXmbh.getText());
		addFfxm.setParam(0, 1, textXmmc.getText());
		addFfxm.setParam(0, 2, textXmmc.getText());
		addFfxm.setParam(0, 3, textDj.getText());
		addFfxm.setParam(0, 4, comboLb.getText());
		addFfxm.setParam(0, 5, textBz.getText());
		addFfxm.setParam(0, 6, btnJy.getSelection());
		addFfxm.setParam(0, 7, btnSfhf.getSelection());
		addFfxm.setParam(0, 8, textHfzq.getText());
		addFfxm.setParam(0, 9, btnSfyhlzq.getSelection());
		addFfxm.setParam(0, 10, textHlzq.getText());
		addFfxm.setParam(0, 11, textFwsj.getText());
	}

	protected void setValues() {
		textXmbh.setText(ffxmSet.getValue("xmbh", String.class, ""));
		textXmmc.setText(ffxmSet.getValue("xmmc", String.class, ""));
		textFwsj.setText(ffxmSet.getValue("fwsj", String.class, ""));
		textBz.setText(ffxmSet.getValue("bz", String.class, ""));
		textDj.setText(ffxmSet.getValue("dj", String.class, ""));
		comboLb.setText(ffxmSet.getValue("lb", String.class, ""));
		btnJy.setSelection(ffxmSet.getValue("jy", Boolean.class, false));
		btnKy.setSelection(!ffxmSet.getValue("jy", Boolean.class, false));
		btnSfhf.setSelection(ffxmSet.getValue("hf", Boolean.class, false));
		btnSfyhlzq.setSelection(ffxmSet.getValue("hlzq", Boolean.class, false));
		textHfzq.setText(ffxmSet.getValue("hfzq", String.class, ""));
		textHlzq.setText(ffxmSet.getValue("hlzq", String.class, ""));
		if (type == Add) {
			nextId.update();
			if (nextId.getCount() > 0) {
				Integer id = nextId
						.getValueForIndex(0, "nextId", Integer.class);
				DecimalFormat df = new DecimalFormat("0000");
				textXmbh.setText(df.format(id));
			}
			textXmmc.setFocus();
		}
	}

	protected void createGUIAfter() {
		fwlb.update();
		nextId.update();
	}
	

}
